﻿using System.Collections;
using System.IO;
using MyWebToolingCollection;
using System;


namespace MyWebToolingCollection
{
    class JudgementInFormMain
    {
        /*
         * 读取文件streamReaderOpenFiles
         * */
        public static ArrayList streamReaderOpenFiles(string filePath)
        {
            //test file: C:\Users\Win7\Desktop\bookmarks.html
            //test files2: F:\C#Projects\RegexForHtmlFiles\RegexForHtmlFiles\bin\Debug\bookmarks.html
            try
            {
                StreamReader objReader = new StreamReader(filePath);
                string sLine = "";
                ArrayList LineList = new ArrayList();
                while (sLine != null)
                {
                    sLine = objReader.ReadLine();
                    if (sLine != null && !sLine.Equals(""))
                        LineList.Add(sLine);
                }
                objReader.Close();

                //string strOut = string.Join("\n", (string[])LineList.ToArray(typeof(string)));
                //return strOut;
                return LineList;
            }
            catch
            {
                return null;
            }

        }

        /*
         * streamReaderWriteFiles
         * StreamReader、Writer系统自带的方式写入文件
         * */
        public static void streamReaderWriteFiles(string strFiles)
        {
            //生成文件或覆盖原文件
            FileStream fs = new FileStream(FormMain.SAVE_FILE_NAME, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            //开始写入
            sw.Write(strFiles);
            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
            fs.Close();
        }

        /*
         * stringFindingForEach
         * 字符串内搜索关键字段
         * */
        public static string stringFindingForEach(string strIn, string left, string right, int count)
        {
            int start, ends;
            string outStr = "";
            start = strIn.IndexOf(left, 0);
            ends = strIn.IndexOf(right, 0);

            //判断值是否有效
            if (ends <= start || (ends < 0 || start < 0))
            {
                return "";
            }
            //判断结束

            switch (FormMain.SUBSTRING_HEAR_INCLUDE)
            {
                case 1:
                    outStr = strIn.Substring(start + left.Length, ends - start + left.Length);
                    break;
                case 2:
                    outStr = strIn.Substring(start, ends - start);
                    break;
                case 3:
                    outStr = strIn.Substring(start, ends - start + right.Length);
                    break;
                default:
                    outStr = strIn.Substring(start + left.Length, ends - start - left.Length);
                    break;//默认为零
            }

            return outStr;
        }


        /*
         * isARealFilesHere
         * 检查字串是否为一个文件
         * */
        public static Boolean isARealFilesHere(string fileUrl)
        {
            if (fileUrl != null && !fileUrl.Equals(""))
            {
                if (File.Exists(fileUrl))
                {
                    return true;
                }
                else
                    return false;
            }
            else
                return false;
        }


        /*
         * MenuStrip
         * 针对MenuStrip控件统一事件处理的方法。传入的controlsNames为控件的名称。
         * 该名称controlsNames即设计时属性Name值。
         * */
        public static void menuStripChgSelectEvents(string controlsNames)
        {
            /*
             * this.HexadecimalToUnicode, this.HexadecimalToOctal, this.HexadecimalToBinary
             * 11,12,13;21,22,23......91,92,93;命名规则，第一项的第一个下拉菜单为11，第N个下拉菜单为1N。
            */
            switch (controlsNames)
            {
                //MainHexadecimalItems 十六进制数处理
                case "HexadecimalToUnicode":
                    FormMain.CODE_OR_ENCODE_METHOD = 11;
                    break;
                case "HexadecimalToOctal":
                    FormMain.CODE_OR_ENCODE_METHOD = 12;
                    break;
                case "HexadecimalToBinary":
                    FormMain.CODE_OR_ENCODE_METHOD = 13;
                    break;
                //MainBinaryNumbers 二进制数处理
                case "BinaryToHexadecimal":
                    FormMain.CODE_OR_ENCODE_METHOD = 21;
                    break;
                case "BinaryToUnicode":
                    FormMain.CODE_OR_ENCODE_METHOD = 22;
                    break;
                //MainBaseSixtyFour Base64编码串处理
                case "ToBaseSixtyFourCoding":
                    FormMain.CODE_OR_ENCODE_METHOD = 31;
                    break;
                case "BaseSixtyFourToUnicode":
                    FormMain.CODE_OR_ENCODE_METHOD = 32;
                    break;
                //MainStringEncoding 常规字符串处理
                case "StringToHexadecimal":
                    FormMain.CODE_OR_ENCODE_METHOD = 41;
                    break;
                case "StringToBaseSixtyFourCode":
                    FormMain.CODE_OR_ENCODE_METHOD = 42;
                    break;
                //default   默认情形
                default:
                    FormMain.CODE_OR_ENCODE_METHOD = 0;
                    break;
            }
        }


    }
}